-- UIShareSelectCN
-- Created by chengb Aug/19/2016
-- 国内分享选择界面

UIShareSelectCN = class("UIShareSelectCN", function()
    return cc.Layer:create();
end);

function UIShareSelectCN.create(shareType, title, desc, args)
    return UIShareSelectCN.new(shareType, title, desc, args);
end

-- 构造函数
function UIShareSelectCN:ctor(shareType, title, desc, args)
    self:setName("UIShareSelectCN");
    local node = cc.CSLoader:createNode("layout/share/ShareSelectCN.csb");
    self:addChild(node);
    self.node = node;

    self.shareType = shareType;
    self.title = title;
    self.desc = desc;
    self.args = args;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIShareSelectCN:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIShareSelectCN:redraw()
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(getLocStr("system_share"));
end

-- 注册点击事件
function UIShareSelectCN:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            closeFormByName("UIShareSelectCN");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册微信分享按钮点击事件
    local btnWeixin = findChildByName(self.node, "CT/btn_weixin");
    local weixinImg = findChildByName(btnWeixin, "icon");
    weixinImg:loadTexture("images/ui/channel/weixin_share_01.png");
    local function onWeixinClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            weixinImg:loadTexture("images/ui/channel/weixin_share_02.png");
        elseif eventType == ccui.TouchEventType.ended then
            weixinImg:loadTexture("images/ui/channel/weixin_share_01.png");
            AudioM.playFx("button_return");

            -- 如果是游戏分享，直接弹sdk界面
            if self.shareType == SHARE_TYPE_GAME then
                ShareM.wechatShare(WECHAT_SCENE_SESSION, self.shareType, {
                    ["desc"] = getLocStr("wecat_game_share_desc"),
                    ["url"]  = ShareM.getCNShareUrl(),
                } )
                closeFormByName("UIShareSelectCN");
                return;
            end

            local uiShare = UIShare.create(SHARE_WAY_WECHAT_SESSION, self.shareType, self.title, self.desc, self.args);
            UIMgr.getCurrentScene():removeFormByName("UIShare");
            UIMgr.getCurrentScene():addForm(uiShare);
            closeFormByName("UIShareSelectCN");
        end
    end
    btnWeixin:addTouchEventListener(onWeixinClick);
    TextStyleM.setTextStyle(btnWeixin, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnWeixin:setTitleText(getLocStr("btn_text_weixin_share"));

    -- 注册朋友圈分享按钮点击事件
    local btnFriends = findChildByName(self.node, "CT/btn_friends");
    local friendsImg = findChildByName(btnFriends, "icon");
    friendsImg:loadTexture("images/ui/channel/friends_share_01.png");
    local function onFriendsClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            friendsImg:loadTexture("images/ui/channel/friends_share_02.png");
        elseif eventType == ccui.TouchEventType.ended then
            friendsImg:loadTexture("images/ui/channel/friends_share_01.png");
            AudioM.playFx("button_return");

            -- 如果是游戏分享，直接打开链接
            if self.shareType == SHARE_TYPE_GAME then
                ShareM.wechatShare(WECHAT_SCENE_FRIENDS, self.shareType, {
                    ["desc"] = getLocStr("wecat_game_share_desc"),
                    ["url"]  = ShareM.getCNShareUrl(),
                });
                closeFormByName("UIShareSelectCN");
                return;
            end

            local uiShare = UIShare.create(SHARE_WAY_WECHAT_FRIENDS, self.shareType, self.title, self.desc, self.args);
            UIMgr.getCurrentScene():removeFormByName("UIShare");
            UIMgr.getCurrentScene():addForm(uiShare);
            closeFormByName("UIShareSelectCN");
        end
    end
    btnFriends:addTouchEventListener(onFriendsClick);
    TextStyleM.setTextStyle(btnFriends, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnFriends:setTitleText(getLocStr("btn_text_friends_share"));

    -- 注册微博分享按钮点击事件
    local btnWeibo = findChildByName(self.node, "CT/btn_weibo");
    local weiboImg = findChildByName(btnWeibo, "icon");
    weiboImg:loadTexture("images/ui/channel/weibo_share_01.png");
    local function onWeiboClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            weiboImg:loadTexture("images/ui/channel/weibo_share_02.png");
        elseif eventType == ccui.TouchEventType.ended then
            weiboImg:loadTexture("images/ui/channel/weibo_share_01.png");
            AudioM.playFx("button_return");

            -- 如果是游戏分享，直接打开链接
            if self.shareType == SHARE_TYPE_GAME then
                self:weiboShareGame();
                return;
            end

            local uiShare = UIShare.create(SHARE_WAY_WEIBO, self.shareType, self.title, self.desc, self.args);
            UIMgr.getCurrentScene():removeFormByName("UIShare");
            UIMgr.getCurrentScene():addForm(uiShare);
            closeFormByName("UIShareSelectCN");
        end
    end
    btnWeibo:addTouchEventListener(onWeiboClick);
    TextStyleM.setTextStyle(btnWeibo, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnWeibo:setTitleText(getLocStr("btn_text_weibo_share"));
end

-- 微博分享游戏
function UIShareSelectCN:weiboShareGame()
    local text = getLocStr("weibo_game_share_desc") .. tostring(ShareM.getCNShareUrl());
    imageName = WEIBO_SHARE_GAME_IMAGE;
    createGameLogoSnapshot(imageName);

    local function callback()
        ShareM.weiboShare(self.shareType, imageName, text);
        closeFormByName("UIShareSelectCN");
    end
    performWithDelay(self, callback, 0.01);
end

-- 注册事件处理回调函数
function UIShareSelectCN:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIShareSelectCN 界面析构清理");
            EventMgr.removeAll("UIShareSelectCN");
        end
    end);
end




